package com.xiaoying.server.publish.servlet;

import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class ServletToBeanProxy extends GenericServlet{

	private static Log log = LogFactory.getFactory().getInstance(ServletToBeanProxy.class);
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String targetBean;
	private Servlet proxy;	
	
	
	 public void init(ServletConfig config) throws ServletException {
		 super.init(config);
		 log.debug("servletToBeanProxy init");
		 this.targetBean = config.getInitParameter("targetBean");
		 getServletBean();
		 proxy.init(getServletConfig());
	 }
	 
	
	@Override
	public void service(ServletRequest servletRequest, ServletResponse servletResponse)
			throws ServletException, IOException {
		 proxy.service(servletRequest, servletResponse);
	}
	
	
	private void getServletBean(){
//		ServletContext servletContext = getServletContext();
//		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
//		this.proxy = (Servlet)wac.getBean(targetBean);
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext-config.xml");
		this.proxy = (Servlet)ctx.getBean(targetBean);		
	}
	
	

}
